﻿using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using Surging.Core.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Repository.Repositorys
{
    /// <summary>
    /// 环信消息历史记录表仓储层
    /// </summary>
    public class HxMsgHistoryRepository : SqlSugarRepository<HxMsgHistoryEntity, Guid>
    {
        private ISugarUnitOfWork _unitOfWork;
        public HxMsgHistoryRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<HxMsgHistoryEntity>> GetPageList(HxMsgHistoryQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<HxMsgHistoryEntity>()
                .Where(p => p.IsDeleted == 0);
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc).ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<HxMsgHistoryEntity>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        /// <summary>
        /// 环信消息Id是否存在
        /// </summary>
        /// <returns></returns>
        public async Task<bool> ExistHxMsgId(string hxMsgId)
        {
            return await _unitOfWork.QueryClient.Queryable<HxMsgHistoryEntity>().Where(it => it.HxMsgId == hxMsgId && it.IsDeleted == 0).CountAsync() > 0;
        }

        /// <summary>
        /// 环信消息Id是否存在
        /// </summary>
        /// <returns></returns>
        public async Task<int> BatchCreate(List<HxMsgHistoryEntity> list)
        {
            return await _unitOfWork.ModifyClient.Insertable(list).ExecuteCommandAsync();
        }
    }
}
